package com.woniuxy.provider.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.woniuxy.commons.annotation.RequirePerms;
import com.woniuxy.commons.entity.Goods;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @Author zhangyong
 * @Date 2021/8/2 0002
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    //获取商品信息
    //仪表盘只会监控使用了HystrixCommand 的
    @RequirePerms("goods:all")
    @GetMapping("/all")
    @HystrixCommand(fallbackMethod = "abc")
    public List<Goods> all() throws Exception{
        //Thread.sleep(3000);
        //返回数据
        return Arrays.asList(
                new Goods(1001,"苹果",1000),
                new Goods(1001,"华为",2000),
                new Goods(1001,"小米",3000),
                new Goods(1001,"三星",4000)
        );
    }

    public List<Goods> abc() throws Exception{
        //Thread.sleep(3000);
        //返回数据
        return Arrays.asList();
    }

    //以id查询商品信息 遵循restful风格
    @GetMapping("/find/{id}")
    public Goods findById(@PathVariable("id") int id){
        //模拟服务器压力大
        try {
            Thread.sleep(5000); //处理时间长，表示服务器大
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println(id);
        return new Goods(1004,"娃娃",3000);
    }

    //POST请求接收数据
    @PostMapping("/add")
    public Boolean add(@RequestBody Goods goods){
        System.out.println(goods);
        //
        return true;
    }

    //put请求
    @PutMapping("/update")
    public Boolean update(@RequestBody Goods goods){
        System.out.println(goods);
        //
        System.out.println("hello");
        return true;
    }

    //delete请求
    @DeleteMapping("/del/{id}")
    public void del(@PathVariable("id") int id){
        System.out.println(id);
    }
}
